package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.user.mapper.UserPaidTrackMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: hsl
 * @CreateTime: 2025-03-10  13:42
 */
@Slf4j
@Service("1002")
public class TrackStrategy implements ItemTypeStrategy {

    @Resource
    private UserPaidTrackMapper userPaidTrackMapper;
    @Resource
    private TrackInfoFeignClient trackInfoFeignClient;

    @Override
    public int savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        // 声明一个变量  进行返回结果赋值
        int result = 0;

        if (SystemConstant.ORDER_ITEM_TYPE_TRACK.equals(userPaidRecordVo.getItemType())) {
            //  买的是声音
            // 避免重复购买声音
            //  购买的声音ID列表是啥
            List<Long> TrackIdFroItemIdList = userPaidRecordVo.getItemIdList();
            //  根据声音ID其获取声音对象
            Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfo(TrackIdFroItemIdList.get(0));
            Assert.notNull(trackInfoResult, "远程调用声音服务获取数据失败");
            TrackInfo trackInfo = trackInfoResult.getData();
            Assert.notNull(trackInfo, "远程调用声音服务获取数据trackInfo失败");
            // 查询 当前用户是否购买过声音
            //  一样的就不需要进行插入  可能会涉及到退款 ；反之则插入数据 即可
            LambdaQueryWrapper<UserPaidTrack> userPaidTrackLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userPaidTrackLambdaQueryWrapper.eq(UserPaidTrack::getUserId, userPaidRecordVo.getUserId())
                    .eq(UserPaidTrack::getTrackId, userPaidRecordVo.getItemIdList().get(0));
            List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(userPaidTrackLambdaQueryWrapper);
            //  转换成我们想要的trackId 集合 用stream流进行转换
            List<Long> userPaidTrackIdList = userPaidTrackList.stream().map(UserPaidTrack::getTrackId).collect(Collectors.toList());
            // 循环遍历  取差集进行插入
            //  交集则进行退款   发送消息即可
            for (Long trackId : TrackIdFroItemIdList) {
                if (userPaidTrackIdList.contains(trackId)) {
                    // 退款
                    log.warn("重复购买进行退款----请注意账户消息" + trackId);
                } else {
                    //  插入数据
                    UserPaidTrack userPaidTrack = new UserPaidTrack();
                    userPaidTrack.setUserId(userPaidRecordVo.getUserId());
                    userPaidTrack.setAlbumId(trackInfo.getAlbumId());
                    userPaidTrack.setTrackId(trackId);
                    userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
                    return result += userPaidTrackMapper.insert(userPaidTrack);
                }
            }
            return result;
        }
        // 数据返回
        return result;
    }
}
